import './Scss/PersonnelPost.scss'
import { Http } from '../../../../Common/Api'
import { useSize, useRequest } from 'ahooks'
import { useState, useRef, useEffect } from 'react'
import { Column } from '@ant-design/plots'
import dayjs from 'dayjs'
import { FrontModalBox } from '../Modal'

export const PersonnelPost001 = () => {
    const { run: geyPersonStatPersonTypeStatAttendance } = useRequest(Http, {
        manual: true,
        onSuccess: res => {
            let list = []
            res.list.forEach(({ tableName, personCount, attendanceCount }) => {
                list = [
                    ...list,
                    { name: '进场人数', type: tableName, value: personCount },
                    { name: '出勤人数', type: tableName, value: attendanceCount },
                ]
            })
            setData(list)
        },
    })
    const [OptionsCall, setOptionsCall] = useState()

    useEffect(() => {
        geyPersonStatPersonTypeStatAttendance({
            url: `getPersonStatPersonTypeStatAttendance`,
            data: { date: dayjs().format('YYYY-MM-DD') },
        })
    }, [geyPersonStatPersonTypeStatAttendance])

    const ChartsBox = useRef()
    const size = useSize(ChartsBox)
    const [data, setData] = useState([])
    const [Charts, setCharts] = useState()

    useEffect(() => {
        if (size) {
            const { width, height } = size
            const config = {
                autoFit: false,
                width,
                height,
                maxColumnWidth: 14,
                appendPadding: 24,
                data,
                isGroup: true,
                xField: 'type',
                yField: 'value',
                seriesField: 'name',
                legend: {
                    layout: 'horizontal',
                    position: 'top',
                    offsetX: -24,
                    itemName: { style: { fill: 'white', fontSize: 12 } },
                },

                xAxis: {
                    label: { style: { fill: '#B2FBFD' } },
                    line: { style: { stroke: '#B2FBFD' } },
                    tickLine: { style: { stroke: '#B2FBFD' } },
                },
                yAxis: {
                    tickCount: 5,
                    label: { style: { fill: '#B2FBFD' } },
                    grid: { line: { style: { stroke: 'rgba(178, 251, 253, 0.2)', lineDash: [8, 2] } } },
                },
            }
            setCharts(config)
        }
    }, [size, data])

    return (
        <div className="PersonnelPost001" ref={ChartsBox}>
            {Charts && (
                <Column
                    {...Charts}
                    onReady={plot =>
                        plot.on('plot:click', () => {
                            setOptionsCall({
                                type: 'TableList',
                                data: {
                                    title: '岗位统计详情',
                                    type: 'SecondaryPersonnelTemplateNew',
                                },
                            })
                        })
                    }
                />
            )}
            <FrontModalBox OptionsCall={OptionsCall} />
        </div>
    )
}
